package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.DomainModel;
import models.OwnerDomainModel;
import models.UserModel;

import entities.Domain;
import entities.OwnerDomain;

public class ProcessOwnerDomainChangeServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
    String strIds = (String) request.getParameter("ids");
    String action = (String) request.getParameter("action");
    int isUpdated = 0;
    String[] ids = strIds.split(",");
    UserModel userModel = UserModel.getInstance();
    
    ArrayList<String> statusAction = new ArrayList<String>(Arrays.asList("status_active"));
    
    OwnerDomainModel ownerModel = OwnerDomainModel.getInstance();
    DomainModel domainModel = DomainModel.getInstance();
    for (int i = 0; i < ids.length; i++) {
        if (!ids[i].isEmpty()) {
        	if(statusAction.contains(action.toLowerCase())) {
        		isUpdated = 1;
                
                OwnerDomain ownerDomain = ownerModel.getOwnerDomain(ids[i]);
                if (ownerDomain != null) {
                	//Update domain info
                	Domain domain = domainModel.get(ownerDomain.getDomain());
                	boolean isChange = false;
                	if (!ownerDomain.getTitle().isEmpty()) {
                		isChange = true;
                		domain.setTitle(ownerDomain.getTitle());
                	}
                	
                	if (!ownerDomain.getDescription().isEmpty()) {
                		isChange = true;
                		domain.setDescription(ownerDomain.getDescription());
                	}
                	
                	if (isChange) {
                		domainModel.insert(domain);
                	}
                	
                	//Update onwer domain
                	ownerDomain.setIsUpdated(isUpdated);
                	ownerModel.insert(ownerDomain);
                }
        	} else if (action.equalsIgnoreCase("remove")) {
        		
			}
        }
    }
}
}
